package com.huanying.goodjob.jobtype;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huanying.framework.BaseDao;
import com.huanying.framework.PageBean;
import com.huanying.framework.utils.MemCachedManager;
import com.huanying.goodjob.skill.Skill;

@Service
public class JobtypeServiceImpl implements JobtypeService {

	@Autowired
	private BaseDao dao;
    private MemCachedManager cache ;
    private Map<String,Object> map;
	@Override
	public void add_jobtype(Jobtype jobtype) throws Exception {
		dao.save(jobtype);
	
		cache=MemCachedManager.getInstance();
          if(cache.get("jobtype")==null)
          {	map=new HashMap<String,Object>();
        	  map.put("jobtype_id"+jobtype.getId(),jobtype);
        	  cache.set("jobtype", map);
          }else
          {
        	  map=(Map<String,Object>)cache.get("jobtype");
        	  map.put("jobtype_id"+jobtype.getId(),jobtype);
        	  cache.set("jobtype", map);
          }
	}

	@Override
	public void update_jobtype(Jobtype jobtype) throws Exception {
		dao.update(jobtype);
		cache=MemCachedManager.getInstance();
		map=new HashMap<String,Object>();
		 if(cache.get("jobtype")!=null)
         {
			  map=(Map<String,Object>)cache.get("jobtype");
        	  map.replace("jobtype_id"+jobtype.getId(),jobtype);
        	  cache.set("jobtype", map);
         }
	}

	@Override
	public int delete_jobtype(int id) throws Exception {

		List<Object> param = new ArrayList<Object>();
		String hql = "update Jobtype set status=0 where id=?";
		param.add(id);
		return dao.exculeByParams(hql, param);
	}

	@Override
	public PageBean search_jobtype(Map<String, Object> map, int pageSize, int page) throws Exception {
		StringBuffer hql = new StringBuffer("from Jobtype where 1=1");
		List<Object> param = new ArrayList<Object>();
		if (map.get("name") != "" && map.get("name") != null) {
			hql.append(" and name like ?");
			param.add("%" + map.get("name") + "%");
		}
		if (map.get("status") != "" && map.get("status") != null && map.get("status") == "可用") {
			hql.append(" and status=?");
			param.add(1);
		}
		if (map.get("status") != "" && map.get("status") != null && map.get("status") == "禁用") {
			hql.append(" and status=?");
			param.add(0);
		}
		int allRow = dao.queryAllRowCount(hql.toString(), param);
		int totalPage = PageBean.countTotalPage(pageSize, allRow);
		final int offset = PageBean.countOffset(pageSize, page);
		final int currentPage = PageBean.countCurrentPage(page);
		List<Object> list = dao.queryForPageAndParams(hql.toString(), param, offset, pageSize);

		// 把分页信息保存到Bean中
		PageBean pageBean = new PageBean();
		pageBean.setPageSize(pageSize);
		pageBean.setCurrentPage(currentPage);
		pageBean.setAllRow(allRow);
		pageBean.setTotalPage(totalPage);
		pageBean.setList(list);
		pageBean.init();

		return pageBean;

	}

	@Override
	public Jobtype getbyId_jobtype(int id) throws Exception {
		String hql = "from Jobtype as j where j.id=? ";
		List<Object> param = new ArrayList<Object>();
		param.add(id);

		return (Jobtype) dao.exculeHqlGetOjbectByParams(hql, param);

	}

	@Override
	public void delete_jobtype_skill(int id, int status) throws Exception {
		// TODO Auto-generated method stub
		List<Object> param = new ArrayList<Object>();
		String hql = "update Skill set status=? where jobtype_id=?";
		param.add(status);
		param.add(id);
		dao.exculeByParams(hql, param);
		
		cache=MemCachedManager.getInstance();
		Skill skill;
		map=new HashMap<String,Object>();
		 if(cache.get("skill")!=null)
         {
			  map=(Map<String,Object>)cache.get("skill");
			  Iterator<Map.Entry<String, Object>> entries = map.entrySet().iterator(); 
			  while (entries.hasNext()) {  
					Map.Entry<String, Object> entry =entries.next();  
					if(((Skill)entry.getValue()).getJobtype().getId()==id)
				    {
						skill=(Skill)entry.getValue();
						skill.setStatus(status);
						map.replace("skill_id"+id,skill);
				    }
			  }
        	  cache.set("skill", map);
         }
	}

	@Override
	public List search_jobtypes() throws Exception {
		// TODO Auto-generated method stub
		String hql="from Jobtype where status=1";
		List<Object> param = new ArrayList<Object>();
		return dao.exculeHqlByParams(hql, param);
	}



}
